requireJS(['constant', 'site', 'handlebars', 'public'], function (Constant, site, Handlebars, Public) {
    var url = {
        selectSqlBackupService: "manager/selectSqlBackupService.do"
    }
    Public.initProductSearch();
    var frontData = {
        pageNum : 1
    }
    //查询订单表
    function salesManCheck() {
        var data = {
            pageNum: frontData.pageNum,
        }
        site.send({
            url: url.selectSqlBackupService,
            data: data,
            callback: function (data) {
                data = JSON.parse(data)
                if (data.type == 1) {
                    if (data.content) {
                        $('.order-page-container').createPager({
                            current: frontData.pageNum,
                            total: data.content.total,
                            pageSize: data.content.pageSize,
                            needInput: false,
                            backFn: function (current) {
                                frontData.pageNum = current;
                                salesManCheck();
                            }
                        })
                        $('.result').html(data.content.total ? data.content.total : 0);
                    }
                    //handlebars模板引擎
                    var source = $("#file_list_tpl").html()
                    var template = Handlebars.compile(source)
                    var content = data.content;

                    var menuIndex = getAurlparam('h') || 0;
                    var thirdMenusIndex = getAurlparam('t') || 0;
                    var leftIndex = getAurlparam('i') || 0;

                    var storageContent = JSON.parse(localStorage.getItem('content'));

                    var obj = {
                        content: content,
                        fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                    }
                    var html = template(obj)
                    $('#file_list').html(html)

                    //复选框一个没选中则全选去除
                    $.each($("input[name='ch_id']"), function (i, e) {
                        var flag = false;
                        $(e).on('click', function () {
                            var _checked = $("input[name='ch_id_all']")
                            var _checkedAll = $("input[name='ch_id']")
                            $.each(_checkedAll, function (ii, ee) {
                                if ($(ee).prop('checked')) {
                                    flag = true
                                }
                                else {
                                    flag = false
                                    return false;
                                }
                            })
                            _checked.prop('checked', flag);
                        })
                    })

                    //查询出委托人列表
                    function queryDelegate(current) {
                        site.send({
                            url: url.ChoiceOfclient,
                            data: {
                                pageNum: current
                            },
                            callback: function (data) {
                                data = JSON.parse(data)
                                if (data.type == 1) {
                                    var source = $("#delegate_role_list_tpl").html();
                                    var template = Handlebars.compile(source);
                                    var list = data.content
                                    var html = template(list)
                                    $('.delegate_role_list').html(html)

                                    $('.delegate-role-page-container').createPager({
                                        current: frontData.pageNum,
                                        total: data.content.total,
                                        pageSize: data.content.pageSize,
                                        needInput: false,
                                        backFn: function (current) {
                                            queryDelegate(current);
                                        }
                                    })
                                }
                                else {
                                    Constant.fn.Message("F", data.msg)
                                }
                            }
                        })
                    }


                    //渲染出模板后在进行操作
                    $(".operations").on('change', function () {
                        var choice = $(this).val();
                        product_id = $(this).attr('product_id');
                        order_id = $(this).attr('order_id');
                        taskId = $(this).attr('task_id');
                        node_type = $(this).attr('node_type')
                        node_id = $(this).attr('node_id')
                        s_id = $(this).attr('s_id');
                        if (choice == 'loanBeforeEditOrder') {
                            pjaxGo("loanBefore/orderEdit.html?order_id=" + order_id + "&product_id=" + product_id + "&task_id=" + taskId + "&node_id=" + node_id + "&s_id=" + s_id);
                        }
                        else if (choice == 'loanBeforeFaceCheckOrder' || choice == 'loanBeforeFacedCheckOrder' || choice == 'loanBeforeReviewCheckOrder' || choice == 'loanBeforeReviewedCheckOrder' || choice == 'loanBeforeTeamReviewCheckOrder' || choice == 'loanBeforeTeamReviewedCheckOrder' || choice == 'loanBeforeSMCheckOrder' || choice == 'loanBeforeSMedCheckOrder' || choice == 'loanBeforeRMCheckOrder' || choice == 'loanBeforeRMedCheckOrder' || choice == 'loanBeforeBossCheckOrder' || choice == 'loanBeforeBossedCheckOrder') {
                            pjaxGo("loanBefore/orderDetail.html?order_id=" + order_id + "&product_id=" + product_id + "&task_id=" + taskId + "&node_id=" + node_id + "&s_id=" + s_id + "&repayment_way=" + repayment_way);
                        }
                        else if (choice == 'submitFace' || choice == 'faceCheckNow' || choice == 'reviewReport' || choice == 'submitCheck' || choice == 'SMCheckNow' || choice == 'RMCheckNow' || choice == 'BossCheckNow') {
                            if (node_type !== 3) {
                                $('.formBox .attachment').remove();
                                $('.formBox .productInfos').remove();
                                $('.formBox .fix').remove();
                                $('input[name="faceResult"]').prop('checked', false);
                                $('.role').remove();
                                $('.chooseRole').remove();
                                $('.approveRemark').val('');
                                $('.processForm_winBox').toggle();
                                getLastNodeId(product_id)
                                /*selectAttachmentType(node_id, product_id)*/
                                queryNodeForm(order_id, node_id, product_id)
                            }
                        }
                        else if (choice == 'backFace' || choice == 'backReview' || choice == 'TeamBackReview' || choice == 'SMBackReview' || choice == 'RMBackReview' || choice == 'BossBackReview' || choice == 'BossedBackReview') {
                            /*odata = order_id;
                            otask_id = taskId;
                            onode_id = node_id;
                            onode_id = node_id;*/
                            arr = [{
                                taskId: taskId,
                                orderId: order_id,
                                nodeId: node_id
                            }]
                            $('.backProcess_winBox').toggle();
                        }
                        else if (choice == '删除申请') {
                            site.send({
                                url: url.delOrderAll,
                                data: {
                                    orderIds: order_id
                                },
                                callback: function (data) {
                                    data = JSON.parse(data)
                                    if (data.type == 1) {
                                        Constant.fn.Message('I', data.msg);
                                        pjaxReload();
                                    } else {
                                        Constant.fn.Message("F", data.msg)
                                    }
                                },
                                callError: function () {

                                }
                            })
                        }
                        else if (choice == 'faceDelegate' || choice == 'reviewDelegate' || choice == 'reviewTeamDelegate' || choice == "reviewSMDelegate" || choice == 'reviewRMDelegate') {
                            $('.delegate_winBox').toggle();
                            queryDelegate(1);
                        }
                        else if ($(this).val() == 'lookProcess') {
                            $('.processBox').remove()
                            site.send({
                                url: url.getActivitiProccessImage,
                                data: {
                                    taskId: taskId,
                                },
                                callback: function (data) {
                                    data = JSON.parse(data)
                                    if (data.type == 1) {
                                        var source = $("#process_list_tpl").html();
                                        var template = Handlebars.compile(source);
                                        var list = data.content
                                        var html = template(list)
                                        $('.lookProcess').append(html)
                                        $('.lookProcess_winBox').toggle();
                                    }
                                    else {
                                        Constant.fn.Message("F", data.msg);
                                    }
                                }
                            })
                            //queryDelegate(1);
                        }
                        $(this).find("option:first").attr("selected", false).attr("selected", true);
                    });

                    Public.record('t_order_public');

                } else {
                    Constant.fn.Message("F", data.msg);
                }
            },
            callError: function () {
                alert("error")
            }
        })
    }

    salesManCheck();
})